package com.atguigu.ssyx.product.controller;


import com.atguigu.ssyx.common.result.Result;
import com.atguigu.ssyx.model.product.Attr;
import com.atguigu.ssyx.model.product.AttrGroup;
import com.atguigu.ssyx.product.service.IAttrService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 商品属性 前端控制器
 * </p>
 *
 * @author author
 * @since 2025-09-13
 */
@RestController
@RequestMapping("/admin/product/attr")
@RequiredArgsConstructor
//@CrossOrigin
@Api(tags = "平台属性管理")
public class AttrController {
    private final IAttrService attrService;
/*
    url: `${api_name}/${groupId}`,
    method: 'get'*/

    @ApiOperation("根据分组id查询属性列表")
    @GetMapping("{groupId}")
    public Result list(@PathVariable Long groupId) {

        LambdaQueryWrapper<Attr> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Attr::getAttrGroupId, groupId);
        List<Attr> list = attrService.list(wrapper);
        return Result.ok(list);
    }

    /*url: `${api_name}/get/${id}`,
    method: 'get'*/

    @ApiOperation("根据id查询属性详情")
    @GetMapping("/get/{id}")
    public Result get(@PathVariable Long id) {
        Attr attr = attrService.getById(id);
        return Result.ok(attr);
    }

    /*url: `${api_name}/save`,
    method: 'post',
    data: role*/

    @ApiOperation("新增商品属性")
    @PostMapping("/save")
    public Result save(@RequestBody Attr attr) {
        attrService.save(attr);
        return Result.ok(null);
    }

  /*  url: `${api_name}/update`,
    method: 'put',
    data: role*/

    @ApiOperation("修改商品属性")
    @PutMapping("/update")
    public Result update(@RequestBody Attr attr) {
        attrService.updateById(attr);
        return Result.ok(null);

    }

    /*url: `${api_name}/remove/${id}`,
    method: 'delete'*/

    @ApiOperation("根据id删除商品属性")
    @DeleteMapping("/remove/{id}")
    public Result remove(@PathVariable Long id) {
        attrService.removeById(id);
        return Result.ok(null);
    }

    /*url: `${api_name}/batchRemove`,
    method: 'delete',
    data: idList*/

    @ApiOperation("批量删除商品属性")
    @DeleteMapping("/batchRemove")
    public Result batchRemove(@RequestBody List<Long> ids) {
        attrService.removeByIds(ids);
        return Result.ok(null);
    }
}
